(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.AIFloatButton = {}));
})(this, (function (exports) { 'use strict';

  let dialog = null;

  function createAIDialog() {
    if (dialog) return;
    dialog = document.createElement('div');
    dialog.className = 'ai-dialog';
    dialog.innerHTML = `
    <div class="ai-dialog-header">AI 对话</div>
    <div class="ai-dialog-body">你好，有什么可以帮您？</div>
  `;
    document.body.appendChild(dialog);
  }

  function toggleAIDialog() {
    if (!dialog) return;
    dialog.classList.toggle('open');
  }

  let isDragging = false;
  let dragOffset = { x: 0, y: 0 };
  let lastPosition = { left: 0, top: 0 };
  let clickPrevented = false;

  function createFloatButton() {
    // 创建按钮
    const btn = document.createElement('div');
    btn.className = 'ai-float-btn';
    btn.innerHTML = '<svg width="28" height="28" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" stroke="white" stroke-width="2"/><path d="M8 12h8M12 8v8" stroke="white" stroke-width="2"/></svg>';
    document.body.appendChild(btn);

    // 初始位置
    let left = window.innerWidth - 80;
    let top = window.innerHeight * 0.7;
    setPosition(left, top);

    // 拖拽事件
    function setPosition(l, t) {
      btn.style.left = l + 'px';
      btn.style.top = t + 'px';
      lastPosition = { left: l, top: t };
    }

    function onPointerDown(e) {
      isDragging = true;
      btn.classList.add('dragging');
      const point = getPoint(e);
      dragOffset.x = point.x - btn.offsetLeft;
      dragOffset.y = point.y - btn.offsetTop;
      clickPrevented = false;
      document.addEventListener('pointermove', onPointerMove);
      document.addEventListener('pointerup', onPointerUp);
    }

    function onPointerMove(e) {
      if (!isDragging) return;
      const point = getPoint(e);
      let l = point.x - dragOffset.x;
      let t = point.y - dragOffset.y;
      // 限制在窗口内
      l = Math.max(0, Math.min(window.innerWidth - btn.offsetWidth, l));
      t = Math.max(0, Math.min(window.innerHeight - btn.offsetHeight, t));
      setPosition(l, t);
      clickPrevented = true;
    }

    function onPointerUp(e) {
      if (!isDragging) return;
      isDragging = false;
      btn.classList.remove('dragging');
      // 自动吸附到左右两侧
      let l = lastPosition.left;
      if (l < window.innerWidth / 2) {
        l = 16;
      } else {
        l = window.innerWidth - btn.offsetWidth - 16;
      }
      setPosition(l, lastPosition.top);
      document.removeEventListener('pointermove', onPointerMove);
      document.removeEventListener('pointerup', onPointerUp);
      setTimeout(() => { clickPrevented = false; }, 100);
    }

    function getPoint(e) {
      if (e.touches && e.touches.length) {
        return { x: e.touches[0].clientX, y: e.touches[0].clientY };
      }
      return { x: e.clientX, y: e.clientY };
    }

    btn.addEventListener('pointerdown', onPointerDown);

    // 点击弹出/隐藏对话框
    btn.addEventListener('click', function (e) {
      if (clickPrevented) return;
      toggleAIDialog();
    });

    // 创建对话框
    createAIDialog();

    // 窗口变化时自动调整位置
    window.addEventListener('resize', function () {
      let l = lastPosition.left;
      let t = lastPosition.top;
      l = Math.max(0, Math.min(window.innerWidth - btn.offsetWidth, l));
      t = Math.max(0, Math.min(window.innerHeight - btn.offsetHeight, t));
      setPosition(l, t);
    });
  }

  window.AIFloatButton = {
    create: createFloatButton
  };

  exports.createFloatButton = createFloatButton;

}));
